/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.dao;

import cn.eclass.entity.Entity;
import cn.eclass.entity.GiftEntity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author fortruth
 */
public class GiftDao extends CommonDao {

    private static final String TABLE_NAME = "gift";

    public GiftDao() {
        super(GiftDao.class.getName());
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {
        GiftEntity gift = new GiftEntity();
        try {
            gift.setId(resultSet.getInt("ID"));
            gift.setDescription(resultSet.getString("DESCRIPTION"));
            gift.setPrice(resultSet.getFloat("PRICE"));
            gift.setPicture(resultSet.getString("PICTURE"));
            gift.setName(resultSet.getString("NAME"));
            gift.setCategory_id(Integer.parseInt(resultSet.getString("CATEGORY_ID")));
            gift.setPoints(resultSet.getInt("POINTS"));
        } catch (SQLException ex) {
            logger.error(ex);
        }
        return gift;
    }

    public int add(GiftEntity entity) {
        String sql = "insert into " + TABLE_NAME
                + "(`PRICE`,`NAME`, `DESCRIPTION`,`PICTURE`,`CATEGORY_ID`, `POINTS`) "
                + "values ("
                + entity.getPrice() + ","
                + entity.getName() + ","
                + "'" + entity.getDescription() + "',"
                + "'" + entity.getPicture() + "',"
                + "'" + entity.getCategory_id() + "',"
                + entity.getPoints()
                + ")";
        return super.insert(sql);
    }

    public boolean update(GiftEntity entity) {
        String sql = "update " + TABLE_NAME + " set "
                + "`PRICE` = " + entity.getPrice() + ","
                + "`DESCRIPTION` = '" + entity.getDescription() + "',"
                + "`PICTURE` = '" + entity.getPicture() + "',"
                + "`POINTS` = " + entity.getPoints()
                + "`NAME` = " + entity.getName()
                + "`CATEGORY_ID` = " + entity.getCategory_id()
                + " where id = " + entity.getId();
        int effectedRows = super.update(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean delete(int id) {
        String sql = "delete from " + TABLE_NAME + " where id = " + id;
        int effectedRows = super.delete(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public GiftEntity get(int id) {
        String sql = "select * from " + TABLE_NAME + " where id = " + id;
        List<GiftEntity> entitys = (List<GiftEntity>) super.select(sql);
        return entitys.get(0);
    }

    public List<GiftEntity> list() {
        String sql = "select * from " + TABLE_NAME;
        return (List<GiftEntity>) super.select(sql);
    }

    public List<GiftEntity> getByCat(int catId) {
        String sql = "select * from " + TABLE_NAME + " where category_id = " + catId;
        return (List<GiftEntity>) super.select(sql);
    }
}
